---
title: Envelop
---

[`envelop`](https://envelop.dev) is a lightweight JavaScript/TypeScript library for customizing the GraphQL execution layer and flow, allowing developers to build, share and collaborate on GraphQL-related plugins while filling the missing pieces in GraphQL implementations.
envelop aims to extend the GraphQL execution flow by adding plugins that enrich the feature set of your application. As an application developer, you can leverage envelop plugins to customize your GraphQL execution flow, based on your needs.
`envelop` plugins hook into specific phases within the GraphQL execution pipeline, and can alter each phase based on your needs.

> With `envelop`, you can easily wrap your common logic, and share it with others.

```ts
import fastify from "fastify";
import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL, sendResult } from "graphql-helix";
import { envelop, useLogger, useSchema, useTiming } from "@envelop/core";
import { schema } from "./schema";

const getEnveloped = envelop({
  plugins: [useSchema(schema), useLogger(), useTiming()],
});
const app = fastify();

app.route({
  method: ["GET", "POST"],
  url: "/graphql",
  async handler(req, res) {
    const { parse, validate, contextFactory, execute, schema } = getEnveloped({ req });
    // Create a generic Request object that can be consumed by Graphql Helix's API
    const request = {
      body: req.body,
      headers: req.headers,
      method: req.method,
      query: req.query,
    };

    // Determine whether we should render GraphiQL instead of returning an API response
    if (shouldRenderGraphiQL(request)) {
      res.type("text/html");
      res.send(renderGraphiQL());
    } else {
      const { operationName, query, variables } = getGraphQLParameters(request);
      // Extract the GraphQL parameters from the request and use custom handlers from envelop to process request
      const result = await processRequest({
        operationName,
        query,
        variables,
        request,
        schema,
        parse,
        validate,
        execute,
        contextFactory,
      });

      sendResult(result, res);
    }
  },
});

app.listen(3000, () => {
  console.log(`GraphQL server is running.`);
});
```
